package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.config.AlipayConfig;
import com.google.gson.Gson;
import com.skyc.entity.Order;
import com.skyc.entity.User;
import com.skyc.service.OrderService;
import com.skyc.service.impl.OrderServiceImpl;
import com.sun.xml.internal.bind.v2.runtime.Location;
import com.sun.xml.internal.txw2.Document;

import javafx.scene.control.Alert;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/ToMyOrderServlet")
public class ToMyOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doPost(req, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		User user=(User) session.getAttribute("user");		 
		OrderService os = new OrderServiceImpl();
		String temp = request.getParameter("temp");
		if(user!=null) {
			String uid=user.getUid()+"";	
		if (temp.equals("all")) {
			List<Order> alllist = (List<Order>) session.getAttribute("alllist");
			request.setAttribute("orderlist", alllist);
			request.getRequestDispatcher("myorder2.jsp").forward(request, response);
		} else if (temp.equals("pay")) {// 待付款
			List<Order> alllist = (List<Order>) session.getAttribute("alllist");
			List<Order> paylist = new ArrayList<Order>();
			for (Order o : alllist) {
				if (o.getOpayed() == 0) {
					paylist.add(o);
				}
			}
			request.setAttribute("orderlist", paylist);
			request.getRequestDispatcher("myorder2.jsp").forward(request, response);
		} else if (temp.equals("emit")) {// 待发货
			List<Order> alllist = (List<Order>) session.getAttribute("alllist");
			List<Order> emitlist = new ArrayList<Order>();
			for (Order o : alllist) {
				if (o.getOsend() == 0 && o.getOpayed() == 1 && o.getOstate() == 0) {
					emitlist.add(o);
				}
			}
			request.setAttribute("orderlist", emitlist);
			request.getRequestDispatcher("myorder2.jsp").forward(request, response);
		} else if (temp.equals("take")) {// 待收货
			List<Order> alllist = (List<Order>) session.getAttribute("alllist");
			List<Order> takelist = new ArrayList<Order>();
			for (Order o : alllist) {
				if (o.getOstate() == 0 && o.getOsend() == 1 && o.getOsend() == 1) {
					takelist.add(o);
				}
			}
			request.setAttribute("orderlist", takelist);
			request.getRequestDispatcher("myorder2.jsp").forward(request, response);
		} else if (temp.equals("eval")) {// 待评价
			List<Order> alllist = (List<Order>) session.getAttribute("alllist");
			List<Order> evallist = new ArrayList<Order>();
			for (Order o : alllist) {
				if (o.getOstate() == 1 && o.getOstate() == 1 && o.getOsend() == 1) {
					evallist.add(o);
				}
			}
			request.setAttribute("orderlist", evallist);
			request.getRequestDispatcher("myorder2.jsp").forward(request, response);
		} else if (temp.equals("shouhuo")) {
			String oid = request.getParameter("oid");
			os.updateOstateByOid(oid);
			int countState = (int) session.getAttribute("countState") - 1;
			int pingjia = (int) session.getAttribute("countpingjia") + 1;
			session.setAttribute("countState", countState);
			session.setAttribute("countpingjia", pingjia);
			List<Order> orderlist = os.queryByUid(uid);
			for (Order c : orderlist) {
				c.getClothes()
						.setCimgurl(c.getClothes().getCimgurl().substring(0, c.getClothes().getCimgurl().indexOf('/')));
			}
			session.setAttribute("alllist", orderlist);
			session.setAttribute("orderlist", orderlist);
			request.getRequestDispatcher("myorder.jsp").forward(request, response);
		} else if (temp.equals("dofukuan")) {
			String oid = request.getParameter("oid");
			List<Integer> oid2=new ArrayList<Integer>();
			oid2.add(Integer.valueOf(oid));
			Order order = os.queryByOid(oid);
			Date date = new Date();
			String out_trade_no = date.getTime() + "" + oid;
			String total_amount = order.getClothes().getCprice()*order.getOnum()+"";
			String subject = order.getClothes().getCname();
			String body = "";
			session.setAttribute("flag", 1);
			session.setAttribute("oid", oid2);
//			request.setAttribute("WIDtotal_amount", total_amount);
//			request.setAttribute("WIDout_trade_no", out_trade_no);
//			request.setAttribute("WIDsubject", subject);
//			request.setAttribute("WIDbody", body);			
			// 跳转取值的JSP 是getParameter
			response.sendRedirect("alipay.trade.page.pay.jsp?WIDout_trade_no=" + out_trade_no + "&WIDtotal_amount="
					+ total_amount + "&WIDsubject=" + subject + "100&WIDbody= ");
//			request.getRequestDispatcher("alipay.trade.page.pay.jsp").forward(request, response);
		} else if (temp.equals("fukuan")) {
			List<Integer> oid=(List<Integer>) session.getAttribute("oid");
			for(Integer c:oid) {
				os.updateOpayedByOid(c+"");
			}
			int countPayed = (int) session.getAttribute("countPayed")-oid.size();
			int countSend = (int) session.getAttribute("countSend")+oid.size();
			session.setAttribute("countPayed", countPayed);
			session.setAttribute("countSend", countSend);
			List<Order> orderlist = os.queryByUid(uid);
			session.setAttribute("orderlist", orderlist);
			for (Order c : orderlist) {
				c.getClothes()
						.setCimgurl(c.getClothes().getCimgurl().substring(0, c.getClothes().getCimgurl().indexOf('/')));
			}
			session.setAttribute("alllist", orderlist);
			session.setAttribute("orderlist", orderlist);
			request.getRequestDispatcher("myorder.jsp").forward(request, response);
		}else if (temp.equals("pingjia")) {
			List<Order> querylist = (List<Order>) session.getAttribute("orderlist");
			String oid = request.getParameter("oid");
			os.deleteByOid(oid);
			int countpingjia = (int) session.getAttribute("countpingjia")-1;
			session.setAttribute("countpingjia", countpingjia);
			List<Order> orderlist = os.queryByUid(uid);
			for (Order c : orderlist) {
				c.getClothes()
						.setCimgurl(c.getClothes().getCimgurl().substring(0, c.getClothes().getCimgurl().indexOf('/')));
			}
			session.setAttribute("alllist", orderlist);
			session.setAttribute("orderlist", orderlist);
			request.getRequestDispatcher("myorder.jsp").forward(request, response);
		}else if (temp.equals("tuihuo")) {
			String oid=request.getParameter("oid");
			String type=request.getParameter("money");
			System.out.println(type);
			os.updateOtuihuoByOid(oid, type);
			List<Order> orderlist = os.queryByUid(uid);
			for (Order c : orderlist) {
				c.getClothes()
						.setCimgurl(c.getClothes().getCimgurl().substring(0, c.getClothes().getCimgurl().indexOf('/')));
			}
			session.setAttribute("alllist", orderlist);
			session.setAttribute("orderlist", orderlist);
			request.getRequestDispatcher("myorder.jsp").forward(request, response);
		}else if (temp.equals("quxiao")) {
			String oid=request.getParameter("oid");
			os.deleteByOid(oid);
			List<Order> orderlist = os.queryByUid(uid);
			for (Order c : orderlist) {
				c.getClothes()
						.setCimgurl(c.getClothes().getCimgurl().substring(0, c.getClothes().getCimgurl().indexOf('/')));
			}
			session.setAttribute("alllist", orderlist);
			session.setAttribute("orderlist", orderlist);
			request.getRequestDispatcher("myorder.jsp").forward(request, response);
		}else if (temp.equals("one")) {
			int countState = 0;
			int countPayed = 0;
			int countSend = 0;
			int pingjia = 0;
			int countall = 0;
			List<Order> orderlist = os.queryByUid(uid);
			for (Order c : orderlist) {
				c.getClothes()
						.setCimgurl(c.getClothes().getCimgurl().substring(0, c.getClothes().getCimgurl().indexOf('/')));
				if (c.getOstate() == 0 && c.getOpayed() == 1 && c.getOsend() == 1) {
					countState++;// 待收货
				}
				if (c.getOpayed() == 0 && c.getOstate() == 0 && c.getOsend() == 0) {
					countPayed++;// 待付款
				}
				if (c.getOsend() == 0 && c.getOpayed() == 1 && c.getOstate() == 0) {
					countSend++;// 待发货
				}
				if (c.getOsend() == 1 && c.getOpayed() == 1 && c.getOstate() == 1&&c.getOtuihuo()!=3) {
					pingjia++;// 待评价
				}
				if(c.getOtuihuo()!=3) {
				countall++;
				}
			}
			session.setAttribute("countall", countall);
			session.setAttribute("countState", countState);
			session.setAttribute("countPayed", countPayed);
			session.setAttribute("countSend", countSend);
			session.setAttribute("countpingjia", pingjia);
			session.setAttribute("alllist", orderlist);
			session.setAttribute("orderlist", orderlist);
			request.getRequestDispatcher("myorder.jsp").forward(request, response);
		} }else {
	    	 out.print("<script>alert('请登录账号')</script>"); 
	    	 }
		 
		out.flush();
		out.close();
	}
}
